import PyCarrara
from PyCarrara import *

class Container(object):
  def __init__(self,carrara_object=None,position=None):
    self.carrara_object = carrara_object
    if carrara_object is not None:
      self.update_position()
    else:
      self.position = position

  def update_position(self):
    if self.carrara_object is not None:
      self.carrara_object.refresh()
      self.position = self.carrara_object.position
      

  def get_intersection(self,position):
     return Vector()

class CylinderContainer(Container):
  def __init__(self,radius=100.0,**args):
    super(CylinderContainer,self).__init__(**args)
    self.radius = radius
    
  def get_intersection(self,position):
    self.update_position()
    diff =position - self.position
    diff.z = 0.0
    if diff.mag <= self.radius:
      return Vector(0.0,0.0,0.0)
    return -diff
    
class SphereContainer(Container):
  def __init__(self,radius=100.0,**args):
    super(SphereContainer,self).__init__(**args)
    self.radius = radius
    
  def get_intersection(self,position):
    self.update_position()
    diff = position - self.position
    if diff.mag <= self.radius:
      return Vector(0.0,0.0,0.0)
    return -diff

class BoxContainer(Container):
  def __init__(self,size=Vector(100.0,100.0,100.0),**args):
    super(BoxContainer,self).__init__(**args)
    self.size = size
    
  def get_intersection(self,position):
    self.update_position()
    v = Vector(0.0,0.0,0.0)
    diff = position.x - self.position.x 
    if diff < 0:			# If less than base.x position
      v.x = -diff
    diff -= self.size.x
    if diff > 0:
      v.x = -diff
  
    diff = position.y - self.position.y 
    if diff < 0:			# If less than base.y position
      v.y = -diff
    diff -= self.size.y
    if diff > 0:
      v.y = -diff
  
    diff =  position.z - self.position.z 
    if diff < 0:			# If less than base.z position
      v.z = -diff
    diff -= self.size.z
    if diff > 0:
      v.z = -diff
    print "containment",position,v
    return v 

    